package com.example.demo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.config.login.Userparam;
import com.example.demo.datasource.multi.dao.Deletemapper;
import com.example.demo.datasource.multi.entity.mytasklistentity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class tasktablelist {


    @Autowired
    com.example.demo.datasource.multi.entity.mytasklistentity mytasklistentity;

    @Autowired
    com.example.demo.datasource.multi.dao.tasklistmapper tasklistmapper;
    @Autowired
    com.example.demo.datasource.multi.dao.usermapper usermapper;

    @ResponseBody
    @RequestMapping("/gettasktable")
    public JSON gettasktablelist(HttpServletRequest request, @RequestBody Map<String,String> params){
        System.out.println(params);

        String where = "";

        if (!(params.get("pkid") == null)){
            where = where + " and a.pkid like '%"+ params.get("pkid")+"%'";
        }
        if (!(params.get("realname") == null)){
            where = where + " and b.realname like '%"+ params.get("realname")+"%'";
        }

        HttpSession session1 = request.getSession();
        //统一拦截（查询当前session是否存在user）(这里user会在每次登录成功后，写入session)
        Userparam user = (Userparam) session1.getAttribute(session1.getId());
        String username= user.getUsername();

        String sql0="select * from user where username = '"+user.getUsername()+"'";

        com.example.demo.datasource.multi.entity.user theuser=usermapper.getuser(sql0);

        if (theuser.getRolevalue().equals("Department")){
            where = where +" and d.college = '"+theuser.getCollege()+"' ";
        }

        String sql="SELECT DISTINCT a.id,a.approvalstatus,c.dictname,a.applicationawards,a.createtime,a.type,a.pkid,b.avatar,b.realname,a.workflownumber," +
                "e.name AS college,f.name AS profession,g.name AS tclass  " +
                "FROM application a " +
                "LEFT JOIN userinfo b ON a.userid = b.userid " +
                "LEFT JOIN dict c on c.`code` = a.approvalstatus " +
                "LEFT JOIN user d on d.id = b.userid " +
                "LEFT JOIN organization e on e.code = d.college " +
                "LEFT JOIN organization f on f.code = d.profession " +
                "LEFT JOIN organization g on g.code = d.tclass " +
                "where 1=1"+where;

        System.out.println(sql);

        List<mytasklistentity> thetasklist= tasklistmapper.gettasklistbyuser(sql);
        System.out.println(thetasklist);

        Map<String, Object> result5 = new HashMap<String,Object>();
        result5.put("code",0);
        result5.put("result",thetasklist);
        result5.put("message","提交成功!");
        result5.put("type","success");

        JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
        System.out.println(result8);

        return result8;

    }

    @Autowired
    Deletemapper deletemapper;

    @ResponseBody
    @RequestMapping("/deleteflow")
    public JSON deleteflow(@RequestBody Map<String,String> params){
        System.out.println(params);
        String sql1  ="delete from application where pkid = '"+params.get("pkid")+"'";
        String sql2  ="delete from workflow where pkid = '"+params.get("pkid")+"'";
        String sql3  ="delete from honors where pkid = '"+params.get("pkid")+"'";

        System.out.println(sql1);
        System.out.println(sql2);
        System.out.println(sql3);
        int rel1 = deletemapper.delete(sql1);
        int rel2 = deletemapper.delete(sql2);
        int rel3 = deletemapper.delete(sql3);
        System.out.println(rel1);
        System.out.println(rel2);
        System.out.println(rel3);

        Map<String, Object> result5 = new HashMap<String,Object>();
        result5.put("code",0);
        result5.put("result",true);
        result5.put("message","删除成功!");
        result5.put("type","success");

        JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
        System.out.println(result8);

        return result8;
    }

}
